﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using CloudClip.Clip;
using CloudClip.HelperMehtod;
using CloudClip.Properties;

namespace CloudClip.LocalFile
{
    public class LocalFileClipProvider : IClipProvider
    {
        public LocalFileClipProvider()
        {
            FileWatcherHelper.CreateWatcher(ClipFolderPath, new FileSystemEventHandler(WatcherEvent));
        }

        private void WatcherEvent(object sender, FileSystemEventArgs e)
        {
            //Thread thread = new Thread(SyncClipList);
            //thread.Start();
        }

        public static string ClipFolderPath
        {
            get { return SettingHelper.GetSetting().ClipFolderPath; }
            set 
            {
                Setting setting = SettingHelper.GetSetting();
                setting.ClipFolderPath = value;
                setting.SaveSetting();
            }
        }

        private List<ClipRow> InfoClipList = new List<ClipRow>(); 

        public bool SetClipData(IClipUnit clipUnit)
        {
            if (string.IsNullOrEmpty(ClipFolderPath))
                return false;
             
            #region 建立ClipROW

            string sFileName = Path.Combine(ClipFolderPath, Guid.NewGuid().ToString());

            using (StreamWriter streamWriter = new StreamWriter(sFileName))
            {
                streamWriter.Write(clipUnit.ToText());
            }

            ClipRow clipRow = new ClipRow();
            clipRow.Name = sFileName;
            clipRow.ClipUnit = clipUnit;

            InfoClipList.Insert(0, clipRow);

            int count = InfoClipList.Count();

            if(count > 20)
            {
                ClipRow infoClip = InfoClipList[count - 1];
                InfoClipList.Remove(infoClip);
            }

            #endregion

            #region 計算是否有超過20個,這動作也許比較耗時, 丟Thread, 還沒考慮交互的時候

            Thread thread = new Thread(CalFileCount);
            thread.Start();

            #endregion

            return true;
        }

        private void CalFileCount()
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(ClipFolderPath);

            IOrderedEnumerable<FileInfo> fileInfos =
                directoryInfo.GetFiles().OrderByDescending(s => s.CreationTime);

            int i = 20;

            foreach (FileInfo fileInfo in fileInfos)
            {
                if (i == 0)
                    fileInfo.Delete();

                i--;
            }
        }

        public IClipUnit GetClipData()
        {
            return null;
        }

        public List<ClipRow> GetClipList()
        {
            return InfoClipList;
        }

        public void SyncClipList()
        {
            if (string.IsNullOrEmpty(ClipFolderPath))
                return;

            List<ClipRow> lst = new List<ClipRow>();
            DirectoryInfo directoryInfo = new DirectoryInfo(ClipFolderPath);

            IOrderedEnumerable<FileInfo> fileInfos = directoryInfo.GetFiles().OrderByDescending(s => s.CreationTime);
            foreach (FileInfo fileInfo in fileInfos)
            {
                ClipRow clipRow = InfoClipList.Where(s => s.Name == fileInfo.FullName).FirstOrDefault();

                if (clipRow != null)
                {
                    lst.Add(clipRow);
                }
                else
                {
                    using (StreamReader streamReader = fileInfo.OpenText())
                    {
                        clipRow = new ClipRow();
                        clipRow.Name = fileInfo.FullName;
                        clipRow.ClipUnit = new TextClipUnit(streamReader.ReadToEnd());
                        lst.Add(clipRow);
                    }
                }
            }
            InfoClipList.Clear();
            InfoClipList = lst;
        }

        public bool CheckWorking()
        {
            return true;
        }
    }
}
